using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

[System.Serializable]
public class PlayerStats
{
    [Header("人物初始属性")]
    public float maxHealth;
    public float health; // SAN值
    public float maxShield=10;
    public float shield; // 精神保护（护盾）
    public float strength = 3; // 力量
    public float moveSpeed; // 移动速度
    public float tempBasicMoveSpeed;//临时移动速度
    public float MoveSpeedMultiplier = 1;//移动速度修正
    
    public float defense; // 防御力
    
    public float critRate; // 额外暴击率
    public float critDamage; // 额外暴击伤害
    
    public float distortionLevel; // 失真度
    
    public bool invulnerable; // 无敌状态
    public float invulerableDuration; // 无敌时间
    
    public float attackSpeed; // 角色攻速
    public float tempAttackSpeed; // 临时攻速
    
    
    public float bulletSpeed;//角色弹速
    public float tempBulletSpeed;//临时弹速
    
    public float bulletSpeedModifier; // 弹速修正
    public float PlayerDamageMultiplier;//玩家易伤系数
    public float PlayerAttackMultiplier;//玩家攻击系数
    public float attackSpeedModifier; // 攻速修正
    
    public int splitCountForPlayer;//子弹分裂数
}

public class Player : MonoBehaviour
{
    public GameManager gameManager;
    public PlayerStats stats; // 玩家属性集合
    public List<Item> basicItems; // 基础道具
    public List<PowerUpItem> powerUpItems; // 增强道具
    public List<FunctionalItem> functionalItems; // 功能道具
    private GunShoot gunShoot; // 引用GunShoot脚本
    public List<BasicItem> consumables; // 消耗品
    public int gold;                           // 金币
    
    private BackpackSystem backpack;// 背包系统引用

    public Text text;
    //调试信息
    [Header("UI对应控件")]
    public Slider healthBar;
    public Slider defenceBar;
    public Slider distortionLevelBar;
    public Text moneyText;
    public Text defenceText;
    public Text healthText;
    
    public Bullet Bullet; 
    
    void Start()
    {
        
        // 获取 BackpackSystem 组件
        backpack = GetComponent<BackpackSystem>();//!!!!! 记得挂载
        
        functionalItems = new List<FunctionalItem>();
        gunShoot = GetComponentInChildren<GunShoot>(); // 获取GunShoot组件
        consumables = new List<BasicItem>();
        
        
        stats.distortionLevel = 0;
        stats.health = stats.maxHealth;
        stats.shield = stats.maxShield;
    }

    // 使用道具
    public void UseItem(Item item)
    {
        if (item is PowerUpItem powerUpItem)
        {
            powerUpItem.ApplyEffect(this);
        }
        else if (item is FunctionalItem functionalItem)
        {
            functionalItem.ApplySpecialEffect(this);
        }
        else if (item is StoryItem storyItem)
        {
            storyItem.ApplyEffect(gameManager);
        }
    }

    // 添加道具到背包
    public void AddToInventory(Item item)
    {
        if (item is FunctionalItem functionalItem)
        {
            functionalItems.Add(functionalItem);
        }
        else if (item is BasicItem consumableItem)
        {
            consumables.Add(consumableItem);
        }
        else if (item is PowerUpItem powerUpItem)
        {
            powerUpItems.Add(powerUpItem);
        }
        else
        {
            basicItems.Add(item);
        }
    }

    // 增加金币
    public void AddGold(int amount)
    {
        gold += amount;
    }

    // 减少金币
    public bool SpendGold(int amount)
    {
        if (gold >= amount)
        {
            gold -= amount;
            return true;
        }
        return false;
    }

    // 检查玩家是否拥有这个功能道具
    public bool HasItem(FunctionalItem item)
    {
        return functionalItems.Contains(item);
    }

    // 获取玩家属性
    public float GetStat(string statName)
    {
        switch (statName)
        {
            case "maxHealth":return stats.maxHealth;
            case "sanValue": return stats.health;
            case "maxShield": return stats.maxShield;
            case "shield": return stats.shield;
            case "strength": return stats.strength;
            case "attackSpeed": return stats.attackSpeed;
            case "bulletSpeed": return stats.bulletSpeed;
            case "moveSpeed": return stats.moveSpeed;
            case "defense": return stats.defense;
            case "critRate": return stats.critRate;
            case "critDamage": return stats.critDamage;
            case "distortionLevel": return stats.distortionLevel;
            case "tempAttackSpeed": return stats.tempAttackSpeed;
            case "attackSpeedModifier": return stats.attackSpeedModifier;
            case "bulletSpeedModifier": return stats.bulletSpeedModifier;
            default: return 0;
        }
    }

    // 设置玩家属性
    public void SetStat(string statName, float value)
    {
        switch (statName)
        {
            case "maxHealth":stats.maxHealth = value;break;
            case "sanValue": stats.health = value; break;
            case "maxShield":stats.maxShield = value;break;
            case "shield": stats.shield = value; break;
            case "strength": stats.strength = value; break;
            case "attackSpeed": stats.attackSpeed = value; break;
            case "bulletSpeed": stats.bulletSpeed = value; break;
            case "moveSpeed": stats.moveSpeed = value; break;
            case "defense": stats.defense = value; break;
            case "critRate": stats.critRate = value; break;
            case "critDamage": stats.critDamage = value; break;
            case "distortionLevel": stats.distortionLevel = value; break;
            case "tempAttackSpeed": stats.tempAttackSpeed = value; break;
            case "attackSpeedModifier": stats.attackSpeedModifier = value; break;
            case "bulletSpeedModifier": stats.bulletSpeedModifier = value; break;
        }
    }

    private void Update()
    {
        
        // 拼接状态信息
        string stateInfo = "玩家状态:\n" +
                           $"金币: {gold}\n" +
                           $"血量: {stats.health}:{stats.maxHealth}\n" +
                           $"护盾: {stats.shield}:{stats.maxShield}\n" +
                           $"力量: {stats.strength}\n" +
                           $"移速: {stats.moveSpeed}\n" +
                           $"防御: {stats.defense}\n" +
                           $"暴击率: {stats.critRate}%\n" +
                           $"暴击伤害: {stats.critDamage}%\n" +
                           $"失真度: {stats.distortionLevel}\n" +
                           $"攻击速度: {stats.attackSpeed}\n" + 
                           $"弹速: {stats.bulletSpeed}";

        
        
        text.text = stateInfo;
        if(stats.health > stats.maxHealth)
            stats.maxHealth = stats.health;

        
        
        Maxshield_ValueChanged();
        UIMaker();
    }

    private void Maxshield_ValueChanged()
    {
        if (stats.maxShield < stats.shield)
        {
            stats.maxShield = stats.shield;
        }
    }
    private void UIMaker()
    {
        //失真度ui
        
        moneyText.text = gold.ToString();
        
        
        //血条ui和护盾ui
        
        healthBar.value = stats.health / stats.maxHealth;
        defenceBar.value = stats.shield / stats.maxShield;
        
        healthText.text = $"{stats.health}/{stats.maxHealth}";
        defenceText.text = $"{stats.shield}/{stats.maxShield}";

        if (stats.distortionLevel >= 500)
        {
            distortionLevelBar.value = 1;
        }
        else
        {
            distortionLevelBar.value = stats.distortionLevel/500;
        }
        
    }
}
